#include <stdio.h>
#include <string.h>

#include <linux/ip.h>
#include <netinet/ip6.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/if_ether.h>

#include <pcap.h>


static void pcap_packet_callback( u_char *args, const struct pcap_pkthdr *header, u_char *packet );

static pcap_t * _pcap_handle = NULL;
static char *_pcap_file = "aaa.cap";
static char pcap_err_buff[PCAP_ERRBUF_SIZE];
static int pcap_datalink_type = 0;

static void pcap_packet_callback( u_char *args, const struct pcap_pkthdr *header, u_char *packet )
{
	const struct ethhdr *eth = ( struct ethhdr * )header;
	struct iphdr *iph = ( struct iphdr * )&header[sizeof( struct ethhdr )];

	printf( "ip.src [ %x ], ip.dst [ %x ] protocol [ %u ].\n", iph->saddr, iph->daddr, iph->protocol );	
}

static void openpcap( void )
{

	_pcap_handle = pcap_open_offline( _pcap_file, pcap_err_buff );

	if ( NULL == _pcap_handle ) {
		printf( "Error:open pcap file err %s.\n", pcap_err_buff );
		return ;
	}

	pcap_datalink_type = pcap_datalink( _pcap_handle );
}

static void closepcap( void )
{
	if ( NULL == _pcap_handle  )
		return ;

	pcap_close( _pcap_handle );

	return ;
}

static void runpcap( void )
{
	if ( NULL == _pcap_handle )
		return ;

	pcap_loop( _pcap_handle, -1, &pcap_packet_callback, NULL );

	return ;	
}

int main( void )
{
	openpcap();

	runpcap();

	closepcap();

	return 0;
	
}
